const { model, sequelize } = require("../../models")
const uuid = require("uuid")
/**
 * 
 * @param {userId, fileListId, downloadFileName = "", downloadSpeed = 0, expirationTime = "", createTime } param0 
 * @returns 
 */
async function insertDownloadFile({ userId, fileListId, downloadFileName = "", downloadSpeed = 0, expirationTime = "", createTime }) {
    var data = await model.downloadfile.create({
        id: uuid.v4(),
        userId, fileListId, downloadFileName, downloadSpeed, expirationTime, createTime, active: 1
    })
    return JSON.parse(JSON.stringify(data))
}
/**
 * 
 * @param {通过id获取} id 
 * @returns 
 */
async function getDownloadFileById(id) {
    var data = await model.downloadfile.findOne({
        where: {
            id,
            active: 1
        }
    })
    return JSON.parse(JSON.stringify(data))
}
/**
 * 
 * @param {通过id数组进行获取} ids 
 * @returns 
 */
async function downloadFileListByIds(ids) {
    var data = await model.downloadfile.findAll({
        where: {
            fileListId: ids,
            active: 1
        },
        order: [
            ['createTime']
        ]
    })
    return JSON.parse(JSON.stringify(data))
}
/**
 * 
 * @param {id, userId} param0 
 * 通过id和userid进行逻辑删除
 * @returns 
 */
async function downloadFileDeleteById({ id, userId }) {
    var data = await model.downloadfile.update({
        active: 0
    }, {
        where: {
            id,
            userId
        }
    })
    return JSON.parse(JSON.stringify(data))
}
/**
 * 通过id，userid，active修改速度，时间和下载文件名称
 */
async function downloadFileUpdateById({ downloadFileName, downloadSpeed, expirationTime, id, userId }) {
    var data = await model.downloadfile.update({
        downloadFileName,
        downloadSpeed,
        expirationTime
    }, {
        where: {
            id,
            userId,
            active: 1
        }
    })
    return JSON.parse(JSON.stringify(data))
}
module.exports = {
    insertDownloadFile,
    getDownloadFileById,
    downloadFileListByIds,
    downloadFileDeleteById,
    downloadFileUpdateById
}